//
//  TTEstimateImageView.m
//  TaoTuo
//
//  Created by LG on 2020/8/13.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTEstimateImageView.h"
#import "TTEstimateImageViewCell.h"
#import "YBImageBrowser.h"

static NSString *cellID = @"cell";

@interface TTEstimateImageView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIView *cycleView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *remarkLabel;
@property (nonatomic, strong) TTEstimateDetailRecordModel * model;
@property (nonatomic, assign) CGFloat collectionViewW;

@end

@implementation TTEstimateImageView

+ (instancetype)createViewWithFrame:(CGRect)frame model:(TTEstimateDetailRecordModel *)model
{
    TTEstimateImageView *view = [[self alloc] initWithFrame:frame];
    
    view.collectionViewW = view.width - 40;
    
    [view setupSubviews];
    
    view.model = model;
    
    return view;
}

- (void)setModel:(TTEstimateDetailRecordModel *)model
{
    _model = model;
    
    self.timeLabel.text = model.CreateTime;
    NSString *remark = [NSString stringWithFormat:@"备注：%@", model.Remark];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:remark];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[remark rangeOfString:@"备注："]];
    self.remarkLabel.attributedText = attr;
    
    [self.collectionView reloadData];
    
    [self setNeedsDisplay];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.Information_Record_PictureVideo_List.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTEstimateImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.model.Information_Record_PictureVideo_List[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *imgs = [NSMutableArray array];
    for (TTEstimateRecordImageInfoModel *model in self.model.Information_Record_PictureVideo_List) {
        if (model.Path.length > 0) {
            YBIBImageData *imageData = [YBIBImageData new];
            imageData.imageURL = [NSURL URLWithString:model.Path];
            [imgs addObject:imageData];
        }
    }
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = imgs;
    browser.currentPage = indexPath.row;
    [browser show];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(3);
        make.top.equalTo(self);
        make.width.height.mas_equalTo(12);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cycleView);
        make.left.equalTo(self.cycleView.mas_right).mas_offset(5);
    }];
    
    CGFloat itemW = (_collectionViewW - 10) / 3.0;
    CGFloat h = ceil(self.model.Information_Record_PictureVideo_List.count / 3.0) * (itemW + 5);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.mas_bottom).mas_offset(9);
        make.left.equalTo(self.timeLabel);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(h);
    }];
    
    [_remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).mas_offset(5);
        make.left.equalTo(self.timeLabel);
        make.right.equalTo(self).mas_offset(-10);
    }];
    
    
    [self layoutIfNeeded];
    self.height = self.remarkLabel.bottom;
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cycleView.mas_bottom);
        make.centerX.equalTo(self.cycleView);
        make.width.mas_equalTo(1);
        make.bottom.equalTo(self).mas_offset(10);
    }];
    
    
}

- (void)setupSubviews
{
    _cycleView = [[UIView alloc] init];
    _cycleView.backgroundColor = UIColorHex(#ECECEC);
    _cycleView.layer.cornerRadius = 6;
//    _cycleView.layer.masksToBounds = YES;
    [self addSubview:_cycleView];
    
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = UIColorHex(#ECECEC);
    [self addSubview:_lineView];
    
    
    _timeLabel = [UILabel labelWithTitle:@"2020-01-10" color:UIColorHex(#666666) fontSize:13];
    [self addSubview:_timeLabel];
    
    
    CGFloat collectionViewW = self.width - 40;
    CGFloat itemW = (collectionViewW - 10) / 3.0;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(itemW, itemW);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 1;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = UIColor.whiteColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[TTEstimateImageViewCell class] forCellWithReuseIdentifier:cellID];
    [self addSubview:_collectionView];
    
    _remarkLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#666666) fontSize:12];
    _remarkLabel.numberOfLines = 0;
    [self addSubview:_remarkLabel];
    
    
}

- (void)setLineHidden:(BOOL)lineHidden
{
    _lineHidden = lineHidden;
    self.lineView.hidden = lineHidden;
}

- (CGFloat)viewHeight
{
    [self layoutIfNeeded];
    
    return self.height;
}

@end
